package com.taobao.txc.resourcemanager;

import com.taobao.txc.a.b.p;
import com.taobao.txc.a.b.u;
import com.taobao.txc.common.LoggerInit;
import com.taobao.txc.common.LoggerWrap;
import com.taobao.txc.common.TxcContext;
import com.taobao.txc.common.c.B;
import com.taobao.txc.common.c.D;
import com.taobao.txc.common.c.E;
import com.taobao.txc.common.config.o;
import com.taobao.txc.common.j;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.timeout.IdleStateEvent;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

@ChannelHandler.Sharable
public class c
  extends p
  implements com.taobao.txc.a.a.b, a
{
  private static final LoggerWrap g = LoggerInit.logger;
  private NioEventLoopGroup h = new NioEventLoopGroup(1);
  private com.taobao.txc.a.a.a t;
  private String u;
  protected volatile Set<String> a = null;
  protected volatile Map<String, String> b = new ConcurrentHashMap();
  protected volatile Map<String, String> c = new ConcurrentHashMap();
  private ConcurrentHashMap<String, Object> v = new ConcurrentHashMap();
  protected ConcurrentHashMap<String, g> d = new ConcurrentHashMap();
  private static c w;
  private String x;
  public static Set<String> e = null;
  public boolean f = false;
  
  public c(ThreadPoolExecutor paramThreadPoolExecutor)
  {
    super(paramThreadPoolExecutor);
  }
  
  public static c a()
  {
    return w;
  }
  
  public static c a(ThreadPoolExecutor paramThreadPoolExecutor)
  {
    if (w == null) {
      w = new c(paramThreadPoolExecutor);
    }
    o.a().b(new d());
    return w;
  }
  
  public void b()
  {
    this.f = o.b(false);
    g.info("RmRpcClient skip vip " + this.f);
    this.m.scheduleAtFixedRate(new e(this), 30L, 5L, TimeUnit.SECONDS);
    c();
    new Thread(new h(this, this)).start();
    super.b();
  }
  
  protected void a(String paramString)
  {
    String str1 = (String)this.b.get(paramString);
    if (str1 == null)
    {
      String str2 = com.taobao.txc.common.util.c.g.b(paramString);
      try
      {
        str1 = o.a().a(str2, "TXC_GROUP", 1000L);
        if (str1 != null)
        {
          this.b.put(paramString, str1);
          this.c.put(str1, paramString);
          g.info(String.format("RmRpcClient put vip:%s ==> %s", new Object[] { paramString, str1 }));
        }
        else
        {
          this.b.put(paramString, "");
        }
      }
      catch (IOException localIOException)
      {
        g.error(com.taobao.txc.common.b.b.a.bd, String.format("get vip failed:%s", new Object[] { paramString }));
      }
    }
  }
  
  protected void c()
  {
    this.a = com.taobao.txc.resourcemanager.b.b.b();
    Iterator localIterator = this.a.iterator();
    while (localIterator.hasNext())
    {
      String str = (String)localIterator.next();
      a(str);
    }
  }
  
  private void f()
  {
    if (this.a != null)
    {
      Iterator localIterator = this.a.iterator();
      while (localIterator.hasNext())
      {
        String str = (String)localIterator.next();
        try
        {
          if ((str != null) && (str.length() > 0))
          {
            a(str);
            f(str);
          }
        }
        catch (Exception localException)
        {
          g.a(com.taobao.txc.common.b.b.s.bd, "can not connect to " + str + " cause:" + localException.getMessage(), localException);
        }
      }
    }
  }
  
  public void channelInactive(ChannelHandlerContext paramChannelHandlerContext)
  {
    if (this.j.isShutdown()) {
      return;
    }
    g.info("channel inactive:" + paramChannelHandlerContext.channel());
    a(paramChannelHandlerContext.channel(), com.taobao.txc.common.d.a(paramChannelHandlerContext.channel().remoteAddress()));
    super.channelInactive(paramChannelHandlerContext);
  }
  
  public void a(long paramLong, ChannelHandlerContext paramChannelHandlerContext, Object paramObject)
  {
    Object localObject = com.taobao.txc.common.d.a(paramChannelHandlerContext.channel().remoteAddress());
    String str = (String)this.c.get(localObject);
    if (str != null) {
      localObject = str;
    }
    this.t.a(paramLong, (String)localObject, paramObject);
  }
  
  public Object a(Object paramObject, long paramLong)
  {
    return super.a(j.b(TxcContext.getCurrentXid()), g(), paramObject, paramLong);
  }
  
  public Object a(String paramString, Object paramObject, long paramLong)
  {
    return super.a(paramString, f(paramString), paramObject, paramLong);
  }
  
  private Channel g()
  {
    String str = null;
    if (!TxcContext.inTxcTransaction()) {
      return null;
    }
    str = j.b(TxcContext.getCurrentXid());
    a(str);
    return f(str);
  }
  
  public short d()
  {
    return (short)com.taobao.txc.common.a.a.a();
  }
  
  public void channelRead(ChannelHandlerContext paramChannelHandlerContext, Object paramObject)
  {
    Object localObject;
    if ((paramObject instanceof u))
    {
      localObject = (u)paramObject;
      if (((u)localObject).f() == com.taobao.txc.a.b.a.b)
      {
        if (g.c()) {
          g.debug("received PONG from " + paramChannelHandlerContext.channel().remoteAddress());
        }
        return;
      }
    }
    if ((((u)paramObject).f() instanceof E))
    {
      localObject = (E)((u)paramObject).f();
      D localD = (D)this.r.remove(Long.valueOf(((u)paramObject).e()));
      int i = localD.a.size();
      for (int j = 0; j < i; j++)
      {
        long l = ((Long)localD.b.get(j)).longValue();
        com.taobao.txc.a.b.b localb = (com.taobao.txc.a.b.b)this.n.remove(Long.valueOf(l));
        if (localb == null) {
          g.info("msg:" + l + " is not found in futures.");
        } else {
          localb.a(localObject.b()[j]);
        }
      }
      return;
    }
    super.channelRead(paramChannelHandlerContext, paramObject);
  }
  
  public void userEventTriggered(ChannelHandlerContext paramChannelHandlerContext, Object paramObject)
  {
    if ((paramObject instanceof IdleStateEvent))
    {
      IdleStateEvent localIdleStateEvent = (IdleStateEvent)paramObject;
      if (localIdleStateEvent == IdleStateEvent.READER_IDLE_STATE_EVENT)
      {
        if ((this instanceof com.taobao.txc.resourcemanager.mt.c)) {
          g.info("MtRmRpcClient channel" + paramChannelHandlerContext.channel() + " idle.");
        } else {
          g.info("RmRpcClient channel" + paramChannelHandlerContext.channel() + " idle.");
        }
        try
        {
          paramChannelHandlerContext.disconnect();
          paramChannelHandlerContext.close();
          com.taobao.txc.common.g.a(paramChannelHandlerContext.channel());
        }
        catch (Exception localException) {}
      }
      if (localIdleStateEvent == IdleStateEvent.WRITER_IDLE_STATE_EVENT) {
        try
        {
          a(paramChannelHandlerContext.channel(), com.taobao.txc.a.b.a.a);
        }
        catch (Throwable localThrowable)
        {
          g.a("", "send request error", localThrowable);
        }
      }
    }
  }
  
  private void a(Channel paramChannel, String paramString)
  {
    try
    {
      Object localObject1 = this.v.get(paramString);
      if (localObject1 == null)
      {
        this.v.putIfAbsent(paramString, new Object());
        localObject1 = this.v.get(paramString);
      }
      synchronized (localObject1)
      {
        g localg = (g)this.d.get(paramString);
        if ((localg != null) && (localg.a.compareTo(paramChannel) == 0))
        {
          this.d.remove(paramString);
          try
          {
            g.info("release channel:" + paramChannel);
            paramChannel.disconnect();
            paramChannel.close();
            com.taobao.txc.common.g.a(paramChannel);
          }
          catch (Throwable localThrowable)
          {
            g.a("", "channel close error", localThrowable);
          }
        }
      }
    }
    catch (Exception localException)
    {
      localException = localException;
      g.a("", "close not active channel", localException);
    }
    finally {}
  }
  
  public String b(String paramString)
  {
    String str = null;
    if ((!this.f) && (this.b != null)) {
      str = (String)this.b.get(paramString);
    }
    return str;
  }
  
  private Channel f(String paramString)
  {
    g localg = (g)this.d.get(paramString);
    if (localg != null)
    {
      if (localg.a.isActive()) {
        return localg.a;
      }
      int i = 0;
      for (i = 0; i < 1000; i++)
      {
        try
        {
          Thread.sleep(10L);
        }
        catch (InterruptedException localInterruptedException) {}
        localg = (g)this.d.get(paramString);
        if (localg == null) {
          break;
        }
        if (localg.a.isActive()) {
          return localg.a;
        }
      }
      if (i == 1000)
      {
        g.warn("channel " + localg.a + " is not active after long wait, close it.");
        a(localg.a, paramString);
      }
    }
    g.info("will connect to " + paramString);
    Object localObject1 = this.v.get(paramString);
    if (localObject1 == null)
    {
      this.v.putIfAbsent(paramString, new Object());
      localObject1 = this.v.get(paramString);
    }
    synchronized (localObject1)
    {
      localg = (g)this.d.get(paramString);
      if ((localg != null) && (localg.a.isActive())) {
        return localg.a;
      }
      String str1 = b(paramString);
      InetSocketAddress localInetSocketAddress;
      if ((str1 != null) && (!str1.isEmpty()))
      {
        localInetSocketAddress = com.taobao.txc.common.d.a(str1);
        g.info(String.format("vip: %s ==> %s", new Object[] { paramString, str1 }));
      }
      else
      {
        localInetSocketAddress = com.taobao.txc.common.d.a(paramString);
      }
      g.info("connect to " + paramString);
      Bootstrap localBootstrap = new Bootstrap();
      ((Bootstrap)((Bootstrap)((Bootstrap)((Bootstrap)((Bootstrap)((Bootstrap)localBootstrap.group(this.h)).channel(NioSocketChannel.class)).remoteAddress(localInetSocketAddress).option(ChannelOption.TCP_NODELAY, Boolean.valueOf(true))).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, Integer.valueOf(10000))).option(ChannelOption.SO_KEEPALIVE, Boolean.valueOf(true))).option(ChannelOption.SO_REUSEADDR, Boolean.valueOf(true))).handler(new f(this));
      Channel localChannel = null;
      ChannelFuture localChannelFuture = localBootstrap.connect();
      try
      {
        localChannelFuture.await(15L, TimeUnit.SECONDS);
        if (localChannelFuture.isCancelled()) {
          throw new com.taobao.txc.common.b.c("connect concelled, can not connect to txc server.");
        }
        if (!localChannelFuture.isSuccess()) {
          throw new com.taobao.txc.common.b.c("connect failed, can not connect to txc server.");
        }
        localChannel = localChannelFuture.channel();
      }
      catch (Exception localException1)
      {
        throw new com.taobao.txc.common.b.c(localException1, "can not connect to txc server.");
      }
      try
      {
        String str2 = this.x == null ? com.taobao.txc.resourcemanager.b.b.c() : this.x;
        g.info("RM will register dbkey:" + str2);
        com.taobao.txc.a.b.e locale = new com.taobao.txc.a.b.e(str2);
        locale.c(this.u);
        locale.a(d());
        Object localObject2 = super.a(null, localChannel, locale, 30000L);
        if ((localObject2 != null) && ((localObject2 instanceof com.taobao.txc.a.b.f)))
        {
          if (((com.taobao.txc.a.b.f)localObject2).d())
          {
            g.info("register RM sucesss. server version:" + ((com.taobao.txc.a.b.f)localObject2).b() + ",channel:" + localChannel);
            if (this.x == null)
            {
              String str3;
              synchronized (com.taobao.txc.resourcemanager.b.b.class)
              {
                this.d.put(paramString, new g(localChannel, com.taobao.txc.common.e.a));
                str3 = com.taobao.txc.resourcemanager.b.b.c();
              }
              if (!str2.equals(str3)) {
                a(paramString, localChannel, str3);
              }
            }
            else
            {
              this.d.put(paramString, new g(localChannel, com.taobao.txc.common.e.b));
            }
            return localChannel;
          }
          g.info("register RM failed. server version:" + ((com.taobao.txc.a.b.f)localObject2).b());
          if (localChannel != null) {
            localChannel.close();
          }
          throw new com.taobao.txc.common.b.c(B.b.a(), "register RM failed.");
        }
        if (localChannel != null) {
          localChannel.close();
        }
        throw new com.taobao.txc.common.b.c(B.b.a(), "can not register RM.");
      }
      catch (Exception localException2)
      {
        g.a(com.taobao.txc.common.b.b.P.bd, "register RM failed.", localException2);
        if (localChannel != null) {
          localChannel.close();
        }
        throw new com.taobao.txc.common.b.c(B.b.a(), "can not register RM.");
      }
    }
  }
  
  public void exceptionCaught(ChannelHandlerContext paramChannelHandlerContext, Throwable paramThrowable)
  {
    g.a(com.taobao.txc.common.b.b.ad.bd, com.taobao.txc.common.d.a(paramChannelHandlerContext.channel().remoteAddress()) + "connect exception. " + paramThrowable.getMessage(), paramThrowable);
    Iterator localIterator = this.d.entrySet().iterator();
    while (localIterator.hasNext()) {
      if (((g)((Map.Entry)localIterator.next()).getValue()).a.compareTo(paramChannelHandlerContext.channel()) == 0)
      {
        localIterator.remove();
        g.info("remove channel:" + paramChannelHandlerContext.channel());
      }
    }
    super.exceptionCaught(paramChannelHandlerContext, paramThrowable);
  }
  
  private void a(String paramString1, Channel paramChannel, String paramString2)
  {
    com.taobao.txc.a.b.e locale = new com.taobao.txc.a.b.e(paramString2);
    locale.c(this.u);
    locale.a(d());
    try
    {
      super.a(null, paramChannel, locale, 0L, false);
    }
    catch (com.taobao.txc.common.b.c localc)
    {
      if ((localc.getMessage() != null) && (localc.getMessage().contains("channel is not writable")) && (paramString1 != null))
      {
        this.d.remove(paramString1);
        g.info("remove channel:" + paramChannel);
      }
      else
      {
        g.a("", "register failed", localc);
      }
    }
    catch (IOException localIOException) {}catch (TimeoutException localTimeoutException) {}
  }
  
  public void c(String paramString)
  {
    g.info("registerNewDbKey dbKey:" + paramString);
    Iterator localIterator;
    synchronized (com.taobao.txc.resourcemanager.b.b.class)
    {
      localIterator = this.d.entrySet().iterator();
    }
    while (localIterator.hasNext())
    {
      ??? = (Map.Entry)localIterator.next();
      String str = (String)((Map.Entry)???).getKey();
      g localg = (g)((Map.Entry)???).getValue();
      if (localg.b.a() == com.taobao.txc.common.e.a.a())
      {
        g.info("registerNewDbKey dbKey:" + paramString);
        a(str, localg.a, paramString);
      }
    }
  }
  
  public void a(com.taobao.txc.a.a.a parama)
  {
    this.t = parama;
  }
  
  public void a(long paramLong, String paramString, Object paramObject)
  {
    g.info("RmRpcClient sendResponse " + paramObject);
    super.a(paramLong, f(paramString), paramObject);
  }
  
  public void d(String paramString)
  {
    this.u = paramString;
  }
  
  public void e(String paramString)
  {
    this.x = paramString;
  }
}


/* Location:           C:\Users\ibm\Downloads\txc-yun-sample (1)\txc-yun-sample\lib\txc-client-2.0.72.jar
 * Qualified Name:     com.taobao.txc.resourcemanager.c
 * JD-Core Version:    0.7.0.1
 */